﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.InfoConfig;

namespace MINERP.BLL.InfoConfig
{
    public  class CustomerPaymentBLL
    {
        public List<CustomerPaymentInfo_Info> GetCustomerPayInfo()
        {
            List<CustomerPaymentInfo_Info> list = new List<CustomerPaymentInfo_Info>();
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<CustomerPaymentInfo> listCustomerPay = enty.CustomerPaymentInfo.ToList();
                foreach (var item in listCustomerPay)
                {
                    CustomerPaymentInfo_Info cuInfo = new CustomerPaymentInfo_Info()
                    {
                        CustomerPaymentId = item.paymentID,
                        CustomerPaymentName = item.paymentName,
                        CustomerPaymentCode = item.paymentCode
                    };
                    list.Add(cuInfo);
                }
            }
            return list;
        }

        public int Add(CustomerPaymentInfo_Info cuInfo)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
                {
                    CustomerPaymentInfo customerInfo = new CustomerPaymentInfo()
                    {
                        paymentCode = cuInfo.CustomerPaymentCode,
                        paymentName = cuInfo.CustomerPaymentName
                    };
                    enty.AddToCustomerPaymentInfo(customerInfo);
                    enty.SaveChanges();
                }
            }
            catch
            {
                ret = 1;
            }
            
            return ret;
        }

        public int Update(CustomerPaymentInfo_Info cuInfo)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
                {
                    CustomerPaymentInfo customerInfo = enty.CustomerPaymentInfo.FirstOrDefault(c => c.paymentID == cuInfo.CustomerPaymentId);
                    customerInfo.paymentCode = cuInfo.CustomerPaymentCode;
                    customerInfo.paymentName = cuInfo.CustomerPaymentName;
                    enty.SaveChanges();
                }
            }
            catch
            {
                ret = 1;
            }
            return ret;
        }

        public int Delete(CustomerPaymentInfo_Info cuInfo)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
                {
                    CustomerPaymentInfo customerInfo = enty.CustomerPaymentInfo.FirstOrDefault(c => c.paymentID == cuInfo.CustomerPaymentId);
                    enty.DeleteObject(customerInfo);
                    enty.SaveChanges();
                }
            }
            catch
            {
                ret = 1;
            }
            return ret;
        }
    }
}
